from orm.entity.Dormitory import Dormitory
from sqlConfig import DBSession


def domitoryAddUser(dormitoryId, userId):
    session = DBSession()
    dormitory = session.query(Dormitory).filter_by(dormitoryId=dormitoryId).first()
    user4 = dormitory.user4
    # 宿舍满了
    if user4 is not None:
        return False
    else:
        user1 = dormitory.user1
        user2 = dormitory.user2
        user3 = dormitory.user3
        if user1 is None:
            dormitory.user1 = userId
        elif user2 is None:
            dormitory.user2 = userId
        elif user3 is None:
            dormitory.user3 = userId
        else:
            dormitory.user4 = userId

    session.add(dormitory)
    session.commit()
    session.close()

    return True


def getAllDormitoryId():
    dormitoryIdList = []
    session = DBSession()
    dormitoryList = session.query(Dormitory).filter_by().all()
    for dormitory in dormitoryList:
        dormitoryIdList.append(dormitory.dormitoryId)
    session.close()
    return dormitoryIdList


def getAllDormitoryInformation():
    session = DBSession()
    dormitoryList = session.query(Dormitory).filter_by().all()
    session.close()
    return dormitoryList


def getDormitoryInformationById(dormitoryId):
    session = DBSession()
    dormitory = session.query(Dormitory).filter_by(dormitoryId=dormitoryId).first()
    session.close()
    return dormitory


def addNewDormitory(dormitoryId):
    session = DBSession()
    checkDormitoryId = session.query(Dormitory).filter_by(dormitoryId=dormitoryId).first()
    # 宿舍号已经存在
    if checkDormitoryId is not None:
        session.close()
        return False
    else:
        dormitory = Dormitory(dormitoryId, None, None, None, None)
        session.delete()
        session.commit()
        session.close()
        return True


def deleteDormitory(dormitoryId):
    session = DBSession()
    checkDormitoryId = session.query(Dormitory).filter_by(dormitoryId=dormitoryId).first()
    # 宿舍号不存在
    if checkDormitoryId is None:
        session.close()
        return False
    else:
        session.delete(checkDormitoryId)
        session.commit()
        session.close()
        return True


def dormitoryDeleteUser(dormitoryId, userId):
    session = DBSession()
    # 在删除user时已经判断过此userId不为None，所以此处无需再判断
    dormitory = getDormitoryInformationById(dormitoryId)
    # 删除userId占位，并把后面的user前移
    if dormitory.user1 == str(userId):
        if dormitory.user4 is not None:
            dormitory.user1 = dormitory.user2
            dormitory.user2 = dormitory.user3
            dormitory.user3 = dormitory.user4
            dormitory.user4 = None
        elif dormitory.user3 is not None:
            dormitory.user1 = dormitory.user2
            dormitory.user2 = dormitory.user3
            dormitory.user3 = None
        elif dormitory.user2 is not None:
            dormitory.user1 = dormitory.user2
            dormitory.user2 = None
        else:
            dormitory.user1 = None

    elif dormitory.user2 == str(userId):
        if dormitory.user4 is not None:
            dormitory.user2 = dormitory.user3
            dormitory.user3 = dormitory.user4
            dormitory.user4 = None
        elif dormitory.user3 is not None:
            dormitory.user2 = dormitory.user3
            dormitory.user3 = None
        else:
            dormitory.user2 = None

    elif dormitory.user3 == str(userId):
        if dormitory.user4 is not None:
            dormitory.user3 = dormitory.user4
            dormitory.user4 = None
        else:
            dormitory.user3 = None

    else:
        dormitory.user4 = None

    session.add(dormitory)
    session.commit()
    session.close()
